package com.us.usws.dao;

import java.util.List;

import net.iwsh.core.db.DBExecutor;

import com.us.usws.model.flat.system.DOSysObj;

public class SysObjDAO {

	/***
	 * get SQLs
	 */
	private static final String SQL_GET_OBJ_BY_ID = "select * from sys_obj where id=?";
	private static final String SQL_GET_ALL_OBJS = "select * from sys_obj";

	/**
	 * CUD SQLs
	 */
	private static final String SQL_INSERT_OBJ = "insert into sys_obj(name,display_name,type_code,seq_num,parent_id,is_valid,url,icon_class,description) values (?,?,?,?,?,?,?,?,?)";
	private static final String SQL_UPDATE_SYSOBJ = "update sys_obj set name=?, display_name=?, type_code=?, seq_num=?, parent_id=?, is_valid=?, link_url=?, icon_cls=?, is_visible=?, is_enable=?, is_expand=?, description=? where id=?";
	private static final String SQL_UPDATE_SYSOBJ_IS_VALID = "update sys_obj set is_valid=? where id=?";
	private static final String SQL_DEL_OBJ = "delete from sys_obj where id=?";

	/**
	 * DBExeutor
	 */
	private DBExecutor dbe = new DBExecutor();

	/**
	 * get obj by id
	 * @param uid
	 * @return
	 */
	public DOSysObj getObj(int id) {
		DOSysObj o = dbe.load(SQL_GET_OBJ_BY_ID, DOSysObj.class, id);
		return o;
	}

	/**
	 * get all obj
	 * @return
	 */
	public List<DOSysObj> getAllObjs() {
		return dbe.list(SQL_GET_ALL_OBJS, DOSysObj.class);
	}

	/**
	 * insert obj
	 * @param obj
	 * @return
	 */
	public int inserObj(DOSysObj obj) {
		long key = dbe.insertWithAutoKey(SQL_INSERT_OBJ, obj.getName(), obj.getDisplayName(), obj.getTypeCode(), obj.getSeqNum(),
				obj.getParentId(), obj.getIsValid(), obj.getLinkUrl(), obj.getIconCls(), obj.getDescription());
		return (int) key;
	}

	/**
	 * Update by PK.
	 */
	public void updateSysObj(DOSysObj sysObj) {
		dbe.execute(SQL_UPDATE_SYSOBJ, sysObj.getName(), sysObj.getDisplayName(), sysObj.getTypeCode(), sysObj.getSeqNum(),
				sysObj.getParentId(), sysObj.getIsValid(), sysObj.getLinkUrl(), sysObj.getIconCls(), sysObj.getIsVisible(),
				sysObj.getIsEnable(), sysObj.getIsExpand(), sysObj.getDescription(), sysObj.getId());
	}

	/**
	 * Update is valid flag by PK.
	 */
	public void updateIsValid(String isValid, Integer id) {
		dbe.execute(SQL_UPDATE_SYSOBJ_IS_VALID, isValid, id);
	}

	/*
	 * delete obj
	 * @param uid
	 */
	public void deleteObj(int id) {
		dbe.execute(SQL_DEL_OBJ, id);
	}

}
